$(function () {
//声明变量
    var pro_dialog =  $("#pro_dialog");
    var pro_form = $("#pro_form");
    var pro_datagrid = $("#pro_datagrid");

    pro_datagrid.datagrid({
        url:'/product/query.do',
        //fit:true,
        fitColumns:true,
        width:1600,
        height:370,
        toolbar:'#pro_toolbar',
        pagination:true,
        rownumber:true,
        singleSelect:true,
        columns:[[
            {field:'id',checkbox:true},
            {field:'classify.classNumber',hidden:true},
            {field:'classifyChild.classNumber',hidden:true},
            {field:'name',title:'商品名称',width:80},
            {field:'sn',title:'商品编码',width:80},
            {field:'costPrice',title:'商品进价',width:80},
            {field:'salePrice',title:'销售单价',width:80},
            {field:'classify.classname',title:'一级类别',width:80,formatter:function (value,row,index) {
                    //console.log(row);
                    //当前对象是否存在
                    return row? row.classify.classname:'';
                }},
            {field:'classifyChild',title:'二级类别',width:80,formatter:function (value,row,index) {
                    //当前对象是否存在
                    return row? row.classifyChild.linkName:'';
                }},
            {field:'shop.name',title:'所属店铺',width:80,formatter:function (value,row,index) {

                    //console.log(value);
                    return row?row.shop.name:'';
                }}
        ]]
    });
    $("#seleClass").combobox({
        panelHeight:'auto',
        textField:'classname',
        valueField:'id',
        url:'/productClassify/listAll.do',
        editble:false,
        required:true
    });
    $("#seleLink").combobox({
        panelHeight:'auto',
        textField:'linkName',
        valueField:'linkId',
        url:'/classifyChild/listAll.do',
        editble:false,
        required:true
        /*onClick:function(){
            var value = $("#seleClass").val();
            console.log(value);
        }*/
    });
    $("#seleClass").combobox({
        onSelect:function (row) {
            if(row){
                $("#seleLink").combobox({
                    url:"/productClassify/selectClassify.do?classNumber="+row.classNumber
                })
            }
        }
    });
    //配置dialog
    pro_dialog.dialog({
        width:290,
        height:400,
        buttons:'#btn',
        close:true,
        onClose:function () {
            //关闭的同时清除表单数据
            pro_form.form('clear');
        }
    });
    $("#linkTest").linkbutton({
        onClick:function (data) {
            $.messager.alert('警告','库存不足,请进货','error');
        } 
    });
    $("#fillerproductstorage").linkbutton({
        onClick:function (data) {
            $.messager.alert('消息','暂未开启,敬请期待！感谢!','info');

        } 
    });

            //发送ajax请求
            $.post('/product/count.do',function (data) {
                //console.log(data);
                $("#bircount").html(data);
            },'json');
            //发送ajax请求
            $.post('/productStock/countByStoreNumber.do?storeNumber='+$("#totalExpense").html(),function (data) {
                $("#sumLessThan10").html(data);
            },'json');



    var methodObj = {
        add:function(){
            //显示进价框
            $("#pro_costPrice").show();
            $("#seleClass").combobox({
                onSelect:function (record) {
                    console.log(record);
                        $("#seleLink").combobox({
                            url:"/productClassify/selectClassify.do?classNumber="+record.classNumber
                        });

                   /*//回显表单的二级类别的信息 单独发送请求查询
                    $.get('/productClassify/selectClassify.do',{classNumber:record.classNumber},function(data){
                        //回显二级类别下拉框 调用combobox的方法setValues
                        console.log(data[0]);
                        $("#seleLink").combobox('setValue',data[0]);
                    },'json');*/

                }
            });
            pro_dialog.dialog('open');
            pro_dialog.dialog('setTitle','新增员工');
        },
        modification:function(){
            //获取一行数据
            var row = pro_datagrid.datagrid('getSelected');
            //console.log(row);
            //隐藏进价框
            $("#pro_costPrice").hide();
            if (!row) {
                //没有选择数据提示用户
                $.messager.alert("温馨提示","至少要选择一条数据","warning");
                return;//终止操作
            }
            //同名匹配,回显表单商品分类的数据
            row["classify.classname"] = row.classify.classname;
            row["classify.classNumber"] = row.classify.classNumber;
            row["classify.type"] = row.classify.type;
            row["classify.id"] = row.classify.id;
            row["classifyChild.linkName"] = row.classifyChild.linkName;
            row["classifyChild.classNumber"] = row.classifyChild.classNumber;
            row["classifyChild.linkId"] = row.classifyChild.linkId;
            row["shop.name"] = row.shop.name;
            row["shop.id"] = row.shop.id;
            row["id"] = row.id;
            pro_form.form('load',row);
            //回显表单的二级类别的信息 单独发送请求查询
            $.get('/productClassify/selectClassify.do',{classNumber:row["classify.classNumber"]},function(data){
                //回显二级类别下拉框 调用combobox的方法setValues
                console.log(data);
                $("#seleLink").combobox('setValues',data);
            },'json');

            //处理好回显数据后,打开编辑框
            pro_dialog.dialog('open');
            //设置标题
            pro_dialog.dialog('setTitle','修改员工');
        },
        save:function(){
            //提交表单
            pro_form.form("submit",{
                url:'/product/saveOrUpdate.do',
                onSubmit:function(param){
                    /*var Classify = $("#seleClass").combobox('getValue');
                    param[classify.id] = Classify;*/
                    //获取下拉框数据
                    var classifyChild = $("#seleLink").combobox('getValues');
                    param[classifyChild.linkId] = classifyChild.linkId;
                    param[classifyChild.linkName] = classifyChild.linkName;
                    param[classifyChild.classNumber] = classifyChild.classNumber;
                    var shop = $("#shopId").combobox('getValues');
                    param[shop.id] = shop.id;
                    param[shop.name] = shop.name;
                    console.log(param);
                },
                success:function (data) {
                    //回调函数:data表示返回数据解析为json
                    data = $.parseJSON(data);
                    if (data.success) {
                        //如果返回结果成功__弹出窗口提示
                        $.messager.alert('温馨提示','保存成功','info',function () {
                            methodObj.cancel();
                            pro_datagrid.datagrid('reload');
                        })

                    }else {
                        //如果保存失败,则弹出窗口提示
                        $.messager.alert('温馨提示',data.msg,'error');
                    }
                }
            });
        },

        cancel: function(){
            //关闭弹出框:
            pro_dialog.dialog('close');

        },
        query:function (){
            //获取查询条件
            var keyword =  $("#keyword").textbox('getValue');

            //通过datagrid 的 load方法传递参数
            pro_datagrid.datagrid('load',{
                keyword:keyword
            });

        }

    };
    //统一绑定事件:
    $("a[data-cmd]").click(function () {
        var methodname = $(this).data("cmd");
        //动态获取调用方法名字,调用方法;
        methodObj[methodname]();
    })
});


//删除

//编辑

//============表单的 按钮 保存/取消=================


//保存密码


//高级查询


//修改密码

